package com.yt.user.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.common.utils.Md5Utils;
import com.yt.sys.models.dto.SysUserDTO;
import com.yt.sys.models.service.ISysUserService;
import com.yt.user.common.config.YmlConfig;
import com.yt.user.mapper.LoginInfoMapper;
import com.yt.user.models.bo.LoginInfoSaveBO;
import com.yt.user.models.dto.CustomerInfoDTO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.service.ICustomerInfoService;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.vo.CustomerInfoVO;
import com.yt.user.models.vo.EmployeeInfoVO;
import com.yt.user.models.vo.LoginInfoVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * 登录信息控制器
 *
 * @author zhou
 * @Date 2025-09-08 19:02:40
 */
@DubboService
public class LoginInfoServiceImpl extends ServiceImpl<LoginInfoMapper, LoginInfoDTO> implements ILoginInfoService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ICustomerInfoService customerInfoService;
    @DubboReference
    private ISysUserService sysUserService;

    @Override
    public boolean add(LoginInfoSaveBO saveBO) {
        LoginInfoDTO dto = new LoginInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    //返回角色信息
    @Override
    public String judgment(LoginInfoSaveBO saveBO) {
        Wrapper<CustomerInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", saveBO.getAccount());
        CustomerInfoDTO dto = customerInfoService.selectOne(wrapper);
        if (dto == null) {
            return "员工";
        }
        return "客户";
    }

    //判断账号是否存在
    @Override
    public LoginInfoVO getLogin(LoginInfoSaveBO saveBO) {
        Wrapper<LoginInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", saveBO.getAccount());
        LoginInfoDTO loginInfoDTO = this.selectOne(wrapper);
        if (loginInfoDTO == null) {
            return null;
        }
        LoginInfoVO loginInfoVO = new LoginInfoVO();
        BeanUtils.copyProperties(loginInfoDTO, loginInfoVO);
        return loginInfoVO;
    }

    //判断客户密码是否正确
    @Override
    public CustomerInfoVO getCustomerLogin(LoginInfoSaveBO saveBO) {
        LoginInfoVO loginInfoVO = getLogin(saveBO);
        if (loginInfoVO == null) {
            return null;
        }
        //创建一个vo对象
        CustomerInfoVO customerInfoVO = new CustomerInfoVO();
        //查询客户的数据库信息
        CustomerInfoDTO customerInfoDTO = customerInfoService.selectById(loginInfoVO.getUserId());
        //判断密码是否正确
        if (loginInfoVO.getPassword().equals(Md5Utils.MD5Encode(saveBO.getPassword(), loginInfoVO.getSalt()))) {
            BeanUtils.copyProperties(customerInfoDTO, customerInfoVO);
            return customerInfoVO;
        }
        return null;
    }

    //判断员工密码是否正确
    @Override
    public EmployeeInfoVO getEmployeeLogin(LoginInfoSaveBO saveBO) {
        LoginInfoVO loginInfoVO = getLogin(saveBO);
        if (loginInfoVO == null) {
            return null;
        }
        //创建一个vo对象
        EmployeeInfoVO employeeInfoVO = new EmployeeInfoVO();
        //查询系统员工的数据库信息
        SysUserDTO sysUserDTO = sysUserService.selectById(loginInfoVO.getUserId());
        //判断密码是否正确
        if (loginInfoVO.getPassword().equals(Md5Utils.MD5Encode(saveBO.getPassword(), loginInfoVO.getSalt()))) {
            BeanUtils.copyProperties(sysUserDTO, employeeInfoVO);
            return employeeInfoVO;
        }
        return null;
    }
}
